package com.netintech.intelligenceInterface.iflytek.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;

/**
 * @author lining
 * @date 2021/5/19 9:05
 */
@Component
public class RestTemplateUtil {
    @Autowired
    private static RestTemplate restTemplate;

    @PostConstruct
    public void  init(){
        restTemplate = new RestTemplate();
    }

    public static<T> String getRestTemplateResult(String url, T t){
        HttpEntity<T> httpEntity = new HttpEntity<>(t, HeadersUtil.getHeader(url));
        ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
        return response.getBody();
    }

    public static<T> String getMultiRestTemplateResult(String url, T t){
        HttpEntity<T> httpEntity = new HttpEntity<>(t, HeadersUtil.getMultiHeader(url));
        ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
        return response.getBody();
    }

}
